#!/bin/bash
#
#********************************************************************
#Author:                songliangcheng
#QQ:                    2192383945
#Date:                  2023-02-18 11:24:05
#FileName：             install-salt.sh
#URL:                   http://blog.mykernel.cn
#Description：          curl -SsL https://gitee.com/slcnx/tools/raw/master/scripts/saltstack/install-salt-master.sh | sed 's/\r//' | bash -s -- --interface 192.168.0.82
#Copyright (C):        2023 All rights reserved
#********************************************************************
#描述:
#--interface 选项必须传递
#--publish_port 选项可以省略
#--ret_port 选项可以省略
#--file_roots 选项可以省略
#--pillar_roots 选项可以省略
#--pillar_opts 选项可以省略


source <(curl -sSLf https://gitee.com/slcnx/tools/raw/master/parse_cmd.sh | sed 's/\r//g')

CONFIG='

key    ,argument    ,isempty    ,desc    

--interface    ,interface    ,0    ,工作在哪个接口

--publish_port    ,publish_port    ,1    ,发布消息; master: firewall-cmd --add-port 4505/tcp

--ret_port    ,ret_port    ,1    ,与minion通信 salt指定此端口返回数据; master: firewall-cmd --add-port 4506/tcp

--file_roots    ,file_roots    ,1    ,基本环境目录

--pillar_roots    ,pillar_roots    ,1    ,基本pillar目录

--pillar_opts    ,pillar_opts    ,1    ,启动pillar

'

parse_cmd $@

echo $interface

: ${publish_port:=4505}
: ${ret_port:=4506}
: ${file_roots:=/data/minion}
: ${pillar_roots:=/data/pillar}
: ${pillar_opts:=True}


echo "$publish_port $ret_port $file_roots $pillar_roots $pillar_opts "



# Download key
sudo curl -fsSL -o /usr/share/keyrings/salt-archive-keyring.gpg https://repo.saltproject.io/py3/ubuntu/$(lsb_release  -rs)/amd64/3004/salt-archive-keyring.gpg
# Create apt sources list file
echo "deb [signed-by=/usr/share/keyrings/salt-archive-keyring.gpg arch=amd64] https://repo.saltproject.io/py3/ubuntu/$(lsb_release  -rs)/amd64/3004 focal main" | sudo tee /etc/apt/sources.list.d/salt.list
sed -i '/cdrom/d' /etc/apt/sources.list
apt update
apt-get install salt-master -y
cat > /etc/salt/master.d/default.conf <<EOF
interface: $interface
publish_port: $publish_port
ret_port: $ret_port
file_recv: True # minio push to master
state_top: top.sls # 高级状态
file_roots:
  base:
  - $file_roots
pillar_roots:
  base:
  - $pillar_roots
pillar_opts: $pillar_opts
#nodegroups:
#  k8s: "L@192.168.0.78,192.168.0.79,192.168.0.80,192.168.0.81,192.168.0.82" # 列表
EOF
systemctl restart salt-master.service
